package com.ruoyi.auth.utils;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class LoginUtils {
    /**
     * 解析手机号
     *
     * @param accessToken
     * @param code
     * @return
     */
    public static String decryptPhoneNumber(String accessToken, String code, RestTemplate restTemplate) {
        // 构造请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 构造请求体，包含 code 参数
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put("code", code);
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);

        // 构造请求地址
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken;
        System.out.println(" "+ url);
        // 发送 POST 请求并获取响应
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String responseBody = responseEntity.getBody();
            System.out.println("---------------------------请求成功，获取手机号响应数据：---------------------------" + responseBody);
            return responseBody;
        } else {
            return "获取微信手机号码失败";
        }
    }

    public static void main(String[] args) {

    }
}
